﻿Public Class Compile
    '' Using default compile option
    Private _compileOption As New CompileOption
    Public Property CompileOption() As CompileOption
        Get
            Return _compileOption
        End Get
        Set(ByVal value As CompileOption)
            _compileOption = value
        End Set
    End Property

    Public Function Complie(ByVal mingw As String, ByVal input As String) As CompileResult
        Dim prc As New Process()
        prc.StartInfo.FileName = mingw & "/g++.exe"

        If CompileOption.Output.Compile Then
            prc.StartInfo.Arguments = "-c """ & input & """ -o """ & CompileOption.Output.OutputFile & """"
        ElseIf CompileOption.Output.Assembly Then
            prc.StartInfo.Arguments = "-S """ & input & """ -o """ & CompileOption.Output.OutputFile & """"
        ElseIf CompileOption.Output.Preprocessing Then
            prc.StartInfo.Arguments = "-E """ & input & """ -o """ & CompileOption.Output.OutputFile & """"
        End If

        prc.StartInfo.EnvironmentVariables("PATH") &= ";" & mingw & ";"
        prc.StartInfo.RedirectStandardError = True
        prc.StartInfo.UseShellExecute = False
        prc.StartInfo.CreateNoWindow = True
        prc.Start()

        Dim result As New CompileResult
        result.Raw = prc.StandardError.ReadToEnd()
        result.ExitCode = prc.ExitCode
        result.Errors = ErrorAnalyzer.GetErrorMessages(result.Raw)
        result.Output = CompileOption.Output.OutputFile

        Return result
    End Function
End Class
